// import GameFacade from "../GameFacade";
import BaseManager from "./BaseManager";
import { utils } from "../utils/CommonUtil";
import { GamePropData } from "../game/Data/GamePropData"
import { LevelData } from "../game/Data/LevelData";
import { CompositeData } from "../game/Data/CompositeData";
import { DialogData } from "../game/Data/DialogData";
import { HintData } from "../game/Data/HintData";
import { LanguageData } from "../game/Data/LanguageData";
import EnumData from "../Enum/EnumData";
import { ProductData } from "../game/Data/ProductData";

export default class ConfigManager extends BaseManager {

    public PlayerName:string = "";

    //#region 游戏道具表
    private _gamePropDatas:  Array<GamePropData> = new Array<GamePropData>();

    /** 通过id 获得对应的道具数据*/
    public GetGamePropDataById(id: number): GamePropData {
        if (this._gamePropDatas == null || this._gamePropDatas.length == 0) {
            return null;
        }
        for (let index = 0; index < this._gamePropDatas.length; index++) {
            let data:GamePropData = this._gamePropDatas[index];
            if(data.id == id){
                return data;
            }
        }
        return null;
    }
    //#endregion

    //#region 关卡数据表
    private _levelDatas: Array<LevelData> = new Array<LevelData>();

    public get allLevelDataList(): Array<LevelData> {
        return this._levelDatas;
    }

    /** 通过关卡数 找到对应的关卡数据*/
    public GetLevelDataByLevelNum(levelNum: number): LevelData {
        let length = this._levelDatas.length;
        for (let i = 0; i < length; i++) {
            let levelData = this._levelDatas[i];
            if (levelData.levelNum == levelNum) {
                return levelData;
            }
        }
        return null;
    }
    //#endregion

    //#region 合成表
    private _compositeDatas: Array<CompositeData> = new Array<CompositeData>();

    /** 合成道具 返回最终合成的道具*/
    public CompositeGameProp(materialGameProps: Array<GamePropData>): GamePropData {
        /** 合成材料道具id*/
        let materialGameIds = new Array<number>();
        let length = materialGameProps.length;
        for (let i = 0; i < length; i++) {
            materialGameIds.push(materialGameProps[i].id);
        }
        /** 遍历合成配表数据 看是否有能合成的*/
        let allCompositeLength = this._compositeDatas.length;
        for (let i = 0; i < allCompositeLength; i++) {
            let compositeData = this._compositeDatas[i];
            /** 如果可以合成 直接返回合成的道具*/
            if (compositeData.IsCanComposite(materialGameIds)) {
                /** 合成的道具id*/
                let compositeId = compositeData.compositeData;
                /** 合成的道具数据*/
                let compositePropData = this.GetGamePropDataById(compositeId);
                return compositePropData;
            }
        }
        return null;
    }

    //#endregion

    //#region 对话框表

    private _dialogDatas: Array<DialogData> = new Array<DialogData>();

    /** 通过id 获得对应的对话框数据*/
    public GetDialogDataById(Id: number): DialogData {
        let length = this._dialogDatas.length;
        for (let i = 0; i < length; i++) {
            let dialogData = this._dialogDatas[i];
            if (dialogData.id == Id) {
                return dialogData;
            }
        }
        return null;
    }

    /** 通关关卡id 找到该关卡一开始进行的对话数据*/
    public GetOpenLevelDialogDatas(levelId: number): Array<DialogData> {
        let dialogDatas = new Array<DialogData>();
        let length = this._dialogDatas.length;
        for (let i = 0; i < length; i++) {
            let data = this._dialogDatas[i];
            if (data.levelId == levelId) {
                dialogDatas.push(data);
            }
        }
        return dialogDatas;
    }

    //#endregion 

    //#region 提示信息表

    private _hintDatas: Array<HintData> = new Array<HintData>();

    /** 通过id 获得对应的提示数据*/
    public GetHintDataById(hintId: number): HintData {
        let length = this._hintDatas.length;
        for (let i = 0; i < length; i++) {
            let hintData = this._hintDatas[i];
            if (hintData.id == hintId) {
                return hintData;
            }
        }
        return null;
    }

    /** 通过关卡 获得这个关卡的提示信息*/
    public GetHintDataByLevel(levelId: number): Array<HintData> {
        let hintDatas = new Array<HintData>();
        let length = this._hintDatas.length;
        for (let i = 0; i < length; i++) {
            let hintData = this._hintDatas[i];
            if (hintData.levelId == levelId) {
                hintDatas.push(hintData);
            }
        }

        /** 根据索引排序*/
        hintDatas.sort(function (a, b) {
            if (a.hintIndex < b.hintIndex) {
                return -1;
            } else if (a.hintIndex = b.hintIndex) {
                return 0;
            } else {
                return 1;
            }
        });

        return hintDatas;
    }

    //#endregion


    //#region 多语言表

    private _languageDatas:Array<LanguageData> = new Array<LanguageData>();

    /** 用来存本地 当前语言的key值*/
    static  CUR_LANGUAGE_TYPE:string = "CUR_LANGUAGE_TYPE";

     /** 当前多语言数据*/
     public get curLanguageData():Array<LanguageData>{
        return this._languageDatas;
     };

      /** 当前语言*/
    public  get curLanugageType():EnumData.LanguageType
    {
        var currentLanguage = window.gameFacade.SDKMng.SDK.GetLocalData(ConfigManager.CUR_LANGUAGE_TYPE,"CHINESE");

        switch (currentLanguage) {
            case "CHINESE":
                return EnumData.LanguageType.CHINESE;
        
            case "TRADITIONCN":
                return EnumData.LanguageType.TRADITIONCN;
            
            case "ENGLISH":
                return EnumData.LanguageType.ENGLISH;

            case "JAPAN":
                return EnumData.LanguageType.JAPAN;
                    
            case "FRENCH":
                return EnumData.LanguageType.FRENCH;

            case "PORTUGUESE":
                return EnumData.LanguageType.PORTUGUESE;
            default:
                return EnumData.LanguageType.CHINESE;
        }
    }

    public set curLanugageType(value:EnumData.LanguageType){
        console.log("设置当前语言为："+value.toString());
        window.gameFacade.SDKMng.SDK.SaveLocalData(ConfigManager.CUR_LANGUAGE_TYPE,value.toString());
    }

     /** 根据key值获取当前语言对应的 语言文字*/
     public  GetLanguageByKey( _key:string):string
     {
         let languageData = this.GetLanguageDataByKey(_key);
         let getStr;
         if (languageData == null)
         {
            getStr = "不存在的多语言key";
            console.error("不存在的多语言key:" + _key);
         }
         else
         {
             switch (this.curLanugageType)
             {
                 case EnumData.LanguageType.CHINESE:
                     getStr = languageData.Chinese;
                     break;
                 case EnumData.LanguageType.ENGLISH:
                     getStr = languageData.English;
                     break;
                 case EnumData.LanguageType.TRADITIONCN:
                     getStr = languageData.TraiditionCn;
                     break;
                 case EnumData.LanguageType.JAPAN:
                     getStr = languageData.Japan;
                     break;               
                 case EnumData.LanguageType.FRENCH:
                     getStr = languageData.French;
                     break;               
                 case EnumData.LanguageType.PORTUGUESE:
                     getStr = languageData.Portuguese;
                     break;
                 default:
                     getStr = "不存在的语言类型";
                     break;
             }
         }

         return getStr.replace("<n", "\n");
     }

      /** 根据key值获取当前对应key值得语言data*/
    public  GetLanguageDataByKey( _key:string):LanguageData
    {
        let languageData = null;
        this.curLanguageData.forEach(data => {
            if (data.key == _key)
            {
                languageData= data;
            }
        });
        return languageData;
    }

    //#endregion

    //#region 商品數據表

    private _productDatas:Array<ProductData> = new Array<ProductData>();

    public get curAllProductDatas():Array<ProductData>{
        return this._productDatas;
    }

     /** 通过id 获得对应的商品数据*/
     public  GetDataById(id:number):ProductData
     {
         let length = this.curAllProductDatas.length;
         for (let i = 0; i < length; i++)
         {
             let productData = this.curAllProductDatas[i];
             if (productData.id == id)
             {
                 return productData;
             }
         }
         return null;
     }

     /** 通过productID获得对应的商品数据*/
     public  GetDataByProductId( productId:string):ProductData
     {
         let length = this.curAllProductDatas.length;
         for (let i = 0; i < length; i++)
         {
             let productData = this.curAllProductDatas[i];
             if (productData.productId == productId)
             {
                 return productData;
             }
         }
         return null;
     }

    //#endregion
  
    /** 是否执行了init*/
    private _bCompleteInit: boolean = false;

    public Init() {
        if (false == this._bCompleteInit) {
            this._bCompleteInit = true;
            let self = this;

            window.gameFacade.ResourceMng.PreloadingRes<Laya.WeakObject>("configfile/GamePropData.json", Laya.Loader.JSON, function (loadedRes: Laya.WeakObject) {

                for (let iterator in loadedRes) {
                    var gamePropData = new GamePropData(loadedRes[iterator]);
                    self._gamePropDatas.push(gamePropData);
                }
                console.log("GamePropData：", self._gamePropDatas);
            });

            window.gameFacade.ResourceMng.PreloadingRes<Laya.WeakObject>("configfile/LevelData.json", Laya.Loader.JSON, function (loadedRes: Laya.WeakObject) {
                self._levelDatas = [];
                for (let iterator in loadedRes) {
                    var levelData = new LevelData(loadedRes[iterator]);
                    self._levelDatas.push(levelData);
                }
                /** 按关卡数 排序*/
                // self._levelDatas.sort(function (a, b) {
                //     if (a.levelNum < b.levelNum) {
                //         return -1;
                //     } else if (a.levelNum = b.levelNum) {
                //         return 0;
                //     } else {
                //         return 1;
                //     }
                // });
                console.log("LevelDatas", self._levelDatas);
            });

            window.gameFacade.ResourceMng.PreloadingRes<Laya.WeakObject>("configfile/CompositeData.json", Laya.Loader.JSON, function (loadedRes: Laya.WeakObject) {
                self._compositeDatas = [];
                for (let iterator in loadedRes) {
                    var compositeData = new CompositeData(loadedRes[iterator]);
                    self._compositeDatas.push(compositeData);
                }
                console.log("compositeData", self._compositeDatas);
            });

            window.gameFacade.ResourceMng.PreloadingRes<Laya.WeakObject>("configfile/DialogData.json", Laya.Loader.JSON, function (loadedRes: Laya.WeakObject) {
                self._dialogDatas = [];
                for (let iterator in loadedRes) {
                    var dialogData = new DialogData(loadedRes[iterator]);
                    self._dialogDatas.push(dialogData);
                }
                console.log("dialogDatas", self._dialogDatas);
            });

            window.gameFacade.ResourceMng.PreloadingRes<Laya.WeakObject>("configfile/HintData.json", Laya.Loader.JSON, function (loadedRes: Laya.WeakObject) {
                self._hintDatas = [];
                for (let iterator in loadedRes) {
                    var hintData = new HintData(loadedRes[iterator]);
                    self._hintDatas.push(hintData);
                }
                console.log("hintDatas", self._hintDatas);
            });

            window.gameFacade.ResourceMng.PreloadingRes<Laya.WeakObject>("configfile/LanguageData.json", Laya.Loader.JSON, function (loadedRes: Laya.WeakObject) {
                self._languageDatas = [];
                for (let iterator in loadedRes) {
                    var languageData = new LanguageData(loadedRes[iterator]);
                    self._languageDatas.push(languageData);
                }
                console.log("languageDatas", self._languageDatas);
            });

            window.gameFacade.ResourceMng.PreloadingRes<Laya.WeakObject>("configfile/ProductData.json", Laya.Loader.JSON, function (loadedRes: Laya.WeakObject) {
                self._productDatas = [];
                for (let iterator in loadedRes) {
                    var productData = new ProductData(loadedRes[iterator]);
                    self._productDatas.push(productData);
                }
                console.log("productDatas", self._productDatas);
            });
        }
    }
}
